package files_controller_with_factory;
import java.io.*;

import org.apache.log4j.Logger;

public class TreeCommandExecutor implements ICommandExecutor{
	private static final Logger cont_log = Logger.getLogger("control");
	private static final Logger logger = Logger.getLogger(Controller.class); 
			
	public void execute(String string){
		String[] str_mas = string.split(" ");
		String path = str_mas[1];
		cont_log.info("Tree of directory...");
		inner_execute(path,1);				
	}
	
	private void inner_execute(String path, int nest){
		File folder = new File(path);
		if (!folder.exists()){
			cont_log.warn("Directory not found: " + path);			
		}else{
			StringBuilder gap = new StringBuilder();
			for (int i = 0; i < nest; i++){
				gap.append(' ');
		   }
			File[] list = folder.listFiles();
			
			for(File file : list){
				if(file.isDirectory()){
					logger.info(gap.toString() + "[+]" + file.getName());	
					System.out.println(gap.toString() + "[+]" + file.getName());
					inner_execute(file.toString(), nest+1);
				}else {System.out.println(gap.toString() + file.getName());}
			}
		}			
	}
	
	private static TreeCommandExecutor instance;
	private TreeCommandExecutor(){		
	}
	public static ICommandExecutor getInstance(){
		if(instance==null){
			instance = new TreeCommandExecutor();
		}
		return instance;
	}
}
